﻿window.noteApp.noteViewModel = (function (ko, datacontext) {
    

    var notes = ko.observableArray(),
        error = ko.observable(),
        addNote = function () {
            var note = datacontext.createNote();
            note.BoardID = config.boardID;
            note.NoteTypeID(1);
            note.IsEditingListTitle(true);
            datacontext.saveNewNote(note)
                .then(addSucceeded)
                .fail(addFailed);

            function addSucceeded() {
                showNote(note);
            }
            function addFailed(jqXHR, textStatus) {
                var err = $.parseJSON(jqXHR.responseText);
                if (err.ModelState && err.ModelState["error"]) {
                    error(err.ModelState["error"]);
                }
                else {
                    error("Invalid data");
                }
                postActions();
            }
        },
        addCustomNote = function () {
            var note = datacontext.createNote();
            note.BoardID = config.boardID;
            note.NoteTypeID(2);
            note.IsEditingListTitle(true);
            datacontext.saveNewNote(note)
                .then(addSucceeded)
                .fail(addFailed);

            function addSucceeded() {
                showNote(note);
            }
            function addFailed(jqXHR, textStatus) {
                var err = $.parseJSON(jqXHR.responseText);
                if (err.ModelState && err.ModelState["error"]) {
                    error(err.ModelState["error"]);
                }
                else {
                    error("Invalid data");
                }
                postActions();
            }
        },
        showNote = function (note) {
            notes.push(note); // Insert new Note at the front
        },
        deleteNote = function (note) {
            if (confirm('Do you really want to delete this note?')) {
                notes.remove(note);
                datacontext.deleteNote(note)
                    .fail(deleteFailed);
                postActions();
            }
            function deleteFailed() {
                showNote(note); // re-show the restored list
            }
        },
            setMasonary = function (elem) {
                $('.content').masonry('appended', $(elem));

            },
             postActions = function () {
                 $('.content').masonry('reload');
             },

    //used to edit description (dialog)
    selectedSection = ko.observable(),
    editDescription = function (section) {
        selectedSection(section);
        if ($(".htmlEditor").length > 0) {
            var $ctrl = $(".htmlEditor");
            var area = new nicEditor({
                iconsPath: config.nicimg,
                buttonList: ['bold', 'italic', 'link', 'ul', 'ol', 'fontSize']
            }).panelInstance($ctrl[0].id);
            var $divEdit = $ctrl.prev().find('.nicEdit-main');
            $ctrl.prev().css({ 'border': 'solid 1px #CCCCCC','margin-bottom':'5px' });
            $divEdit.focus();            
        }
    },
    allowToAddNote = ko.computed(function () {
        return (config.tn == 0 || notes().length <= config.tn)
    });

    datacontext.getThemeList().then( //To load config Themes
    datacontext.getNotes(notes, error)); // load Notes

    return {
        notes: notes,
        error: error,
        addNote: addNote,
        addCustomNote : addCustomNote,
        deleteNote: deleteNote,
        selectedSection: selectedSection,
        editDescription: editDescription,
        allowToAddNote: allowToAddNote,
        setMasonary: setMasonary,
        postActions: postActions
    };

})(ko, noteApp.datacontext);

// Initiate the Knockout bindings
ko.applyBindings(window.noteApp.noteViewModel);


